package com.hamster.crm.web;

import com.hamster.crm.pojo.Materiel;
import com.hamster.crm.pojo.MaterielConfig;
import com.hamster.crm.service.MaterielService;
import com.hamster.crm.util.ExcelUtiles;
import com.hamster.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Tarry
 * @create 2019/12/24 10:14
 */
@RestController
@RequestMapping("materiel")
public class MaterielController {
    @Autowired
    private MaterielService materielService;

    /**
     * 添加物料类型
     * @return
     */
    @PostMapping("insertConfig")
    public ResponseEntity<Void> insertConfig(@RequestBody MaterielConfig materielConfig){
        materielService.insertConfig(materielConfig);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id 删除物料类型
     * @param ids id集合
     * @return
     */
    @PostMapping("deleteConfig")
    public ResponseEntity<Void> deleteConfig(@RequestBody List<String> ids){
        materielService.deleteConfig(ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询 物料类型列表
     * @return
     */
    @GetMapping("listConfig")
    public ResponseEntity<List<MaterielConfig>> listConfigs(){
        List<MaterielConfig> list = materielService.listConfigs();
        return ResponseEntity.ok(list);
    }

    /**
     * 添加物料
     * @param materiel
     * @return
     */
    @PostMapping("insertMateriel")
    public ResponseEntity<Void> insertMateriel(@RequestBody Materiel materiel){
        materielService.insertMateriel(materiel);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改物料
     * @param materiel
     * @return
     */
    @PostMapping("updateMateriel")
    public ResponseEntity<Void> updateMateriel(@RequestBody Materiel materiel){
        materielService.updateMateriel(materiel);
        return ResponseEntity.ok().build();
    }

    /**
     *  根据id删除物料
     * @param ids id集合
     * @return
     */
    @PostMapping("deleteMateriel")
    public ResponseEntity<Void> deleteMateriel(@RequestBody List<String> ids){
        materielService.deleteMateriel(ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 物料列表，条件查询,分页
     * @return
     */
    @GetMapping("listMateriel")
    public ResponseEntity<PageResult<Materiel>> listMateriels(
            @RequestParam(required=false) Integer pageSize,
            @RequestParam(required=false) Integer currentPage,
            @RequestParam(required = false) String title
    ){
        PageResult<Materiel> list =materielService.listMateriels(pageSize,currentPage,title);

        return ResponseEntity.ok(list);
    }


    //TODO 物料导出 excel 搁置
    /**
     * 导出全部物料数据
     * @param response
     */
    @RequestMapping("export")
    public void export(HttpServletResponse response){
        List<Materiel> list =  materielService.queryAllMateriel();
        //操作
        ExcelUtiles.exportExcel(list,"物料列表","物料",Materiel.class,"物料信息.xls",response);
    }



}
